package com.fy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fy.mapper.UserMapper;
import com.fy.mapper.UserSignMapper;
import com.fy.pojo.entity.User;
import com.fy.pojo.entity.UserSign;
import com.fy.pojo.vo.other.CheckInVO;
import com.fy.service.UserSignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Slf4j
@Service
public class UserSignServiceImpl extends ServiceImpl<UserSignMapper, UserSign> implements UserSignService {

  @Autowired
  private UserMapper userMapper;

  @Override
  public boolean checkIn(Integer userId) {
    // 1. 检查今天是否已经签到
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date todayStart = calendar.getTime();

    calendar.add(Calendar.DAY_OF_MONTH, 1);
    Date todayEnd = calendar.getTime();

    UserSign todaySign = this.lambdaQuery()
        .eq(UserSign::getUserId, userId)
        .ge(UserSign::getSignTime, todayStart)
        .lt(UserSign::getSignTime, todayEnd)
        .one();

    if (todaySign != null) {
      return false; // 今天已经签到过了
    }

    // 2. 创建签到记录
    UserSign userSign = new UserSign();
    userSign.setUserId(userId);
    userSign.setSignTime(new Date());

    // 3. 保存签到记录
    return this.save(userSign);
  }

  @Override
  public CheckInVO getDailyCheck(Integer userId) {
    // 1. 获取今天的签到记录
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date todayStart = calendar.getTime();

    calendar.add(Calendar.DAY_OF_MONTH, 1);
    Date todayEnd = calendar.getTime();

    UserSign todaySign = this.lambdaQuery()
        .eq(UserSign::getUserId, userId)
        .ge(UserSign::getSignTime, todayStart)
        .lt(UserSign::getSignTime, todayEnd)
        .one();

    // 2. 构建返回对象
    CheckInVO checkInVO = new CheckInVO();
    checkInVO.setChecked(todaySign != null);

    // 3. 获取用户注册时间到现在的天数
    User user = userMapper.selectById(userId);
    if (user != null && user.getCreateTime() != null) {
      Date createTime = user.getCreateTime();
      Date now = new Date();
      long diffInMillies = Math.abs(now.getTime() - createTime.getTime());
      long totalDays = diffInMillies / (24 * 60 * 60 * 1000);
      checkInVO.setUseDays((int) totalDays + 1); // +1 包含注册当天
    } else {
      checkInVO.setUseDays(0);
    }

    // 4. 计算连续签到天数
    int continuousDays = calculateContinuousDays(userId);
    checkInVO.setContinuousDays(continuousDays);

    return checkInVO;
  }

  /**
   * 计算连续签到天数
   */
  private int calculateContinuousDays(Integer userId) {
    int days = 0;
    Date currentDate = new Date();

    while (true) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(currentDate);
      calendar.set(Calendar.HOUR_OF_DAY, 0);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MILLISECOND, 0);
      Date dayStart = calendar.getTime();

      calendar.add(Calendar.DAY_OF_MONTH, 1);
      Date dayEnd = calendar.getTime();

      UserSign sign = this.lambdaQuery()
          .eq(UserSign::getUserId, userId)
          .ge(UserSign::getSignTime, dayStart)
          .lt(UserSign::getSignTime, dayEnd)
          .one();

      if (sign == null) {
        break;
      }

      days++;
      calendar.setTime(dayStart);
      calendar.add(Calendar.DAY_OF_MONTH, -1);
      currentDate = calendar.getTime();
    }

    return days;
  }
}
